package ${service_impl_package_name};


import com.doubo.common.model.CommonPageResult;
import com.doubo.common.model.response.CommonResponse;
import com.doubo.common.util.ConverterUtil;
import com.sibu.shop.common.manage.ResponseManage;
import ${dao_package_name}.${table_name}Mapper;
import ${request_package_name}.${table_name}Request;
import ${request_package_name}.${table_name}PageRequest;
import ${response_package_name}.${table_name}Response;
import ${service_interface_package_name}.${table_name}Service;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Map;
import java.util.stream.Collectors;


/**
* 描述：${table_annotation} 服务实现层
* @author ${author}
* @date ${date}
*/
@Slf4j
@Service("${table_name?uncap_first}Service")
public class ${table_name}ServiceImpl implements ${table_name}Service {

    @Autowired
    private ${table_name}Mapper ${table_name?uncap_first}Mapper;

    /**
    * 分页查询
    */
    @Override
    public CommonResponse<CommonPageResult<${table_name}Response>> listForPage(${table_name}PageRequest request){

        List<${table_name}Response> list = ${table_name?uncap_first}Mapper.select${table_name}ForPage(request);

        return ResponseManage.success(ConverterUtil.convertToPageResult(list,request.getPage()));

    }
    /**
    * 查询全部
    */
    @Override
    public CommonResponse<List<${table_name}Response>> listAll(${table_name}Request request){

        List<${table_name}Response> list = ${table_name?uncap_first}Mapper.select${table_name}All(request);

        return ResponseManage.success(list);

    }

    /**
    * 详细查询
    */
    @Override
    public CommonResponse<${table_name}Response> selectDetail(${table_name}Request request){

        ${table_name}Response response = ${table_name?uncap_first}Mapper.select${table_name}ByRequest(request);
        return ResponseManage.successIfNotNull(response,"没有该记录");

    }

    /**
    * 保存
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResponse<Boolean> saveByRequest(${table_name}Request request){
        int i ;
        if(request.getId() == null ){
            i = ${table_name?uncap_first}Mapper.insert${table_name}ByRequest(request);
        }else{
            i = ${table_name?uncap_first}Mapper.update${table_name}ByRequest(request);
        }

        if(i>0){
            return ResponseManage.success();
        }
        return ResponseManage.fail("保存记录失败");
    }

    /**
    * 删除
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResponse<Boolean> deleteByRequest(${table_name}Request request){
        int i = ${table_name?uncap_first}Mapper.delete${table_name}ByRequest(request);
        if(i>0){
            return ResponseManage.success();
        }
        return ResponseManage.fail("删除记录失败");

    }

    /**
    * 根据id集合返回MAP
    */
    @Override
    CommonResponse<Map<Long,${table_name}Response>> selectMapByIds(List list){

        List<${table_name}Response> repList = ${table_name?uncap_first}Mapper.select${table_name}ByIds(list);
        Map<Long, ${table_name}Response> collect = repList.stream().collect(Collectors.toMap(${table_name}Response::getId, obj -> obj));
        return ResponseManage.success(collect);
    };
}